﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.JCI.Bll;
using Microsoft.JCI.Common.Entity;
using System.Collections.Generic;
using Microsoft.JCI.Common.Utility; 

namespace Microsoft.JCI.Portal.Dispatch
{
    public partial class AgentReDispatch :BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false ;
            }
        }
        public Guid DispatchFormId
        {
            get {
                if (Request.QueryString["DispatchFormId"] != null)
                {
                    return new Guid(Request.QueryString["DispatchFormId"]);
                }
                else
                    return Guid.Empty;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["DispatchFormId"] != null)
                {
                    LoadData();
                }
            }
        }

        private void LoadData()
        {
            DispatchBll patchbll = new DispatchBll();
            DisDispatchFormEntity entity = patchbll.Get<DisDispatchFormEntity>(DispatchFormId);
            if (entity == null)
            {
                btnSave.Enabled = false;
                return;
            }

            SystemBll bll = new SystemBll();
            MadCustomerEntity tMadCustomerEntity = new MasterDataBll().Get<MadCustomerEntity>(entity.CustomerId);
            if (tMadCustomerEntity != null)
            {
                MasterDataBll mdBll = new MasterDataBll();
                List<SysCompanyEntity> list = mdBll.GetCompanyListFromCacheByProvince(tMadCustomerEntity.Province);
                ddlBranch.DataSource = list;
                ddlBranch.DataTextField = "Name";
                ddlBranch.DataValueField = "CompanyCode";
                ddlBranch.DataBind();

                //if (rblSendTo.SelectedValue != "")
                //{
                //    if (Convert.ToInt32(rblSendTo.SelectedValue) == (int)Enumerator.CompanyType.Vendor)
                //    {
                //        LoadVendor();
                //    }
                //}
            }

            ddlBranch.SelectedValue = entity.AllocatedBranch;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlBranch.SelectedValue == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "err", "alert('请选择办事处!');", true);
                return;
            }
            DispatchBll patchbll = new DispatchBll();
            DisDispatchFormEntity entity = patchbll.Get<DisDispatchFormEntity>(DispatchFormId);
            if (entity == null) return;
            entity.AllocatedBranch = ddlBranch.SelectedValue;
            entity.FormStatus = (int)Enumerator.FormStatus.Dispatched;
            entity.DispatchStatus = (int)Enumerator.DispatchStatus.DispatchedBranch;

            patchbll.Update(entity);

            ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('保存成功!');CloseWindow();", true);

        }
    }
}
